package com.waypolice.manager.controller;

import java.util.*;

import com.waypolice.manager.service.PermissionsService;
import com.waypolice.pojo.UpmsPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 权限控制管理
 * @author zhangxiaoyang
 *
 */
@Controller
@Api(value = "权限控制管理", description = "权限控制管理")
@RequestMapping(value="/pcwindow")
public class PermissionsAction {

	private static final Logger LOGGER = LoggerFactory.getLogger(PermissionsAction.class);
	
	@Autowired
	private PermissionsService permissionsService;

	/**
	 * 查询所有权限
	 * @returnTree
	 */
	@ApiOperation(value = "查询组织机构数")
	@RequestMapping(value = "/permissionTree")
	@ResponseBody
	public List<Object>  permissionTree() {
		List<UpmsPermission> upmsPermissionList = permissionsService.findAll();
		//一级菜单结构
		List<Object> result = iterationTree(upmsPermissionList);
		return result;
	}

	private List<Object> iterationTree(List<UpmsPermission> upmsPermissionList) {
		List<Object> result = new ArrayList<>();
		for (UpmsPermission upmsPermission:upmsPermissionList) {
			Map<String,Object> map = new HashMap<>();
			if(upmsPermission.getPid() == 0){
				map.put("text",upmsPermission.getName());
				map.put("Pid",upmsPermission.getPid());
				map.put("id",upmsPermission.getPermissionId());
                map.put("nodes",iterationTreeChild(upmsPermissionList,upmsPermission.getPermissionId()));
            }
		}
		return result;
	}

    private List<Object> iterationTreeChild(List<UpmsPermission> result,Integer permissionId) {
        //二级菜单
        List<Object> twoList = new ArrayList<>();
        for (UpmsPermission upmsPermissionChild:result) {
            Map<String,Object> mapTwo = new HashMap<>();
            if(upmsPermissionChild.getPid() != null && upmsPermissionChild.getPid().equals(permissionId)){
                mapTwo.put("text",upmsPermissionChild.getName());
                mapTwo.put("Pid",upmsPermissionChild.getPid());
                mapTwo.put("id",upmsPermissionChild.getPermissionId());
				twoList.add(mapTwo);
            }
        }
        return twoList;
    }


}
